var Main = {
	nameRexp: new RegExp(/^[\u4e00-\u9fa5]{2,8}$/),//名字2-8位汉字
	isIE8: navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/8./i)=="8.",
	isIE9: navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.match(/9./i)=="9.",
	/**
	 *全局处理ajax
	 *@param Object opt  传入ajax对象参数
	*/
	ajaxUrl: {},
	$ajax: function(opt) {
		if (Main.ajaxUrl[opt.url]) {
			return false;
		}
		Main.ajaxUrl[opt.url] = true;
		opt.cache = true;
		opt.dataType = opt.dataType?opt.dataType:"json";
		opt.type = opt.type?opt.type:"POST";
		opt.error = function(XMLHttpRequest, textStatus) {
			var status = XMLHttpRequest.status;
			if (status == 0)
				return;
			else if (status == 500)
				alert("服务器错误");
			else if (status == 404)
				alert("请求没有找到");
		};
		opt.goSuccess = opt.success;
		opt.success = function(res) {
			if (opt.goSuccess)
				opt.goSuccess(res);
		}
		opt.complete = function() {
			delete Main.ajaxUrl[opt.url];
		}
		$.ajax(opt);
	},
	/**
	 *校验11位手机号
	 *@param int phone  int类型手机号
	*/
	checkPhone: function(phone) {
	    var phoneRexp = new RegExp(/^1[3|4|5|7|8][0-9]{9}$/);
	    if (!phoneRexp.test(phone) || phone == "")
	        return false;
	    return true;
	},
	/**
	 *截取url参数值
	 *@param String name  传入要截取的参数名
	*/
	getQueryString: function(name) { 
	    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null)
	        return unescape(r[2]);
	    return null;
	},
	/**
	 *模拟睡眠
	 *@param int numberMillis  传入毫秒值
	*/
	sleep: function(numberMillis) {
	　　var now = new Date();
	　　var exitTime = now.getTime() + numberMillis;
	　　while (true) {
	    　　now = new Date();
	    　　if (now.getTime() > exitTime)
	    　　    return;
	　　}
	},
	jump: function(url){
		window.location.href = url;
	},
	offsetDate: function(UTCTime) {
		if (UTCTime == "" || UTCTime == undefined)
			return "";
		var now = new Date();
		var date = new Date(UTCTime);

		var offsetMS = parseInt((now.getTime() - date.getTime()) / 1000 / 60);
		if (offsetMS < 5)
			return "刚刚";
		else if (offsetMS < 60)
			return offsetMS + "分钟前";
		else if (now.pattern("yyyy-MM-dd") == date.pattern("yyyy-MM-dd"))
			return date.pattern("HH:mm");
		else if (now.pattern("yyyy") == date.pattern("yyyy"))
			return date.pattern("M月d日")
		else
			return date.pattern("yyyy年M月d日");
	}
};
var Res = {
	opt: null,
	gnTitle: function(opt) {
		Res.opt = opt;
		var html = '';
		if (opt.leftFn)
			Res.leftFn = opt.leftFn;
		else
			Res.leftFn = WinClose;
		Res.rightFn = opt.rightFn;
		Res.switchFn = opt.switchFn;
		// var content = "";
		if (opt.isSwitch) {
		    html += '<div id="header" class="ub c_title bc_base">'+
				    '    <div id="leftBtn" class="uw4 ub ub-ac" ontouchstart="zy_touch()" onclick="Res.leftClick();">'+
				    '        <div class="uw1 uh2 ub-img im_back uml_1"></div>'+
				    '    </div>';
			// content += '{"content":[';
			html += '<div class="ub-f1 up_title ub_center">';
			for (var i=0; i<opt.items.length; ++i) {

				html += '<div id="'+ opt.items[i].url +'" class="up_title_item" ontouchstart="zy_touch()" onclick="Res.switch(\''+ opt.items[i].url +'\')">'+ opt.items[i].title +'</div>';
				// html += '<div id="title'+ i +'" class="up_title ub-f1 uf1p1 ut-s tx-c" ontouchstart="zy_touch()" onclick="Res.switch(\''+ opt.items[i].url +'\')">'+ opt.items[i].title +'</div>';
				// content += '{"inPageName":"p'+ i +'","inUrl":"'+ opt.items[i].url +'","inData":""}';
				// if (i != opt.items.length-1)
				// 	content += ',';
			}
			html += '</div>';
			// content += ']}';
			html +=	'    <div id="rightBtn" class="uw4 ub_center" ontouchstart="zy_touch()" onclick="Res.rightClick();">';
			if (opt.rightTitle)
				html += '	<div class="ub ub-ac ub-pc uh1p6 hide bc_base c_wht1" style="padding:0.3em 0.5em; border-radius:0.2em;background-color:#FFFFFF;line-height:100%;">'+ opt.rightTitle +'</div>';//font-size:14px; 
			else
				html += '	<div class="uw2 ub ub-ac ub-pc uh1p6 ub_img im_'+ opt.rightImg +' hide"></div>';

			html += '    </div>'+
				    '</div>';
		} else {
		    html += '<div id="header" class="ub c_title bc_base">'+
				    '    <div id="leftBtn" class="uw4 ub ub-ac" ontouchstart="zy_touch()" onclick="Res.leftClick();">'+
				    '        <div class="uw1 uh2 ub-img im_back uml_1"></div>'+
				    '    </div>'+
				    '    <div id="title" class="up_title ub-f1 uf1p1 ut-s tx-c">'+ opt.title +'</div>'+
				    '    <div id="rightBtn" class="uw4 ub_center" ontouchstart="zy_touch()" onclick="Res.rightClick();">';
					if (opt.rightTitle)
						html += '	<div class="ub ub-ac ub-pc uh1p6 bc_base c_wht1" style="padding:0.3em 0.5em; border-radius:0.2em;background-color:#FFFFFF;line-height:100%;">'+ opt.rightTitle +'</div>'; //font-size:14px; line-height:100%;
					else
						html += '	<div class="uw2 ub ub-ac ub-pc uh1p6 ub_img im_'+ opt.rightImg +'"></div>';
			html += '    </div>'+
				    '</div>';
		}
		$("body").prepend(html);
	},
	leftClick: function() {
		Res.leftFn();
	},
	rightClick: function() {
		if (Res.rightFn)
			Res.rightFn();
	},
	switch: function(url) {
        Zycon(url, $("#header").outerHeight());
        $(".up_title_item").removeClass("selected");
        $("#"+url).addClass("selected");
    }
};
/*city城市手机控件,用法： 
*$('#id').sliderNav({items:['autobots','decepticons'], arrows: false});
*/
$.fn.sliderNav = function(options) {
	var defaults = { items: ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"], debug: false, height: null, arrows: true};
	var opts = $.extend(defaults, options); var o = $.meta ? $.extend({}, opts, $$.data()) : opts; var slider = $(this); $(slider).addClass('slider');
	$('.slider-content li:first', slider).addClass('selected');
	$(slider).append('<div class="slider-nav"><ul></ul></div>');
	for(var i in o.items) $('.slider-nav ul', slider).append("<li><a alt='#"+o.items[i]+"'>"+o.items[i]+"</a></li>");
	var height = $('.slider-nav', slider).height();
	if(o.height) height = o.height;
	// $('.slider-content, .slider-nav', slider).css('height',height);
	if(o.debug) $(slider).append('<div id="debug">Scroll Offset: <span>0</span></div>');
	$('.slider-nav a', slider).mouseover(function(event){
		var target = $(this).attr('alt');
		var cOffset = $('.slider-content', slider).offset().top;
		var tOffset = $('.slider-content '+target, slider).offset().top;
		var height = $('.slider-nav', slider).height(); if(o.height) height = o.height;
		var pScroll = (tOffset - cOffset) - height/8;
		$('.slider-content li', slider).removeClass('selected');
		$(target).addClass('selected');
		$('.slider-content', slider).stop().animate({scrollTop: '+=' + pScroll + 'px'});
		if(o.debug) $('#debug span', slider).html(tOffset);
	});
	if(o.arrows){
		$('.slider-nav',slider).css('top','20px');
		$(slider).prepend('<div class="slide-up end"><span class="arrow up"></span></div>');
		$(slider).append('<div class="slide-down"><span class="arrow down"></span></div>');
		$('.slide-down',slider).click(function(){
			$('.slider-content',slider).animate({scrollTop : "+="+height+"px"}, 500);
		});
		$('.slide-up',slider).click(function(){
			$('.slider-content',slider).animate({scrollTop : "-="+height+"px"}, 500);
		});
	}
};
/*封装touch事件为tap,用法： 
*$('id').tap(function(dom){
	alert($(dom).text() + "`````");
});
*/
;(function($){
	$.fn.tap = function(callback){
		var $this = this;
		var startTx, startTy;
 
		$(this)[0].addEventListener( 'touchstart', function( e ){
		  	var touches = e.touches[0];
		 
		  	startTx = touches.clientX;
		  	startTy = touches.clientY;
		}, false );
		 
		$(this)[0].addEventListener( 'touchend', function( e ){
		  	var touches = e.changedTouches[0],
		    endTx = touches.clientX,
		    endTy = touches.clientY;
		 
		  	// 在部分设备上 touch 事件比较灵敏，导致按下和松开手指时的事件坐标会出现一点点变化
		  	if( Math.abs(startTx - endTx) < 6 && Math.abs(startTy - endTy) < 6 ){
		    	callback&&callback($this);
		  	} else 
		  		console.log("no touchend");
		}, false );
	}
})(jQuery);



/*
*选项卡tab切换插件，用法： 
* new Tabs({
*	tabsId:"modular_2_tabs",//tabs  ID
*	consId:"modular_2_sins" //cons  ID
* });
*/
;(function(window,$,undefined){
	var defaults = {
		tabsId: "tabs",//tab容器ID
		consId: "cons"//内容ID
	}
	var Tabs = function(jsonData){
		var _this = this;

		this.data = $.extend(defaults,jsonData);
		this.scroll = null;

		if(this.data.tabsId)
			this.tabsId = this.data.tabsId;
		else
			return console.log("请填写选项tab ID");
		if(this.data.consId)
			this.consId = this.data.consId;
		else
			return console.log("请填写内容con ID");

		this.domInit();
		this.eventInit();
	}
	Tabs.prototype = {
		domInit: function(){
			$("#" + this.tabsId + ">div:eq(" + 0 + ")").addClass("stab active").siblings().addClass("stab");
			$("#" + this.consId + ">div:eq(" + 0 + ")").addClass("scon active").siblings().addClass("scon");
		},
		eventInit: function(){
			var _this = this;
			$("body").on("mouseover","#" + _this.tabsId + ">div",function(){
				var $con = $("#" + _this.consId);
				var i = $(this).index();
				$(this).addClass("active").siblings().removeClass("active");
				$con.children("div:eq(" + i + ")").addClass("active").siblings().removeClass("active");
			});
		}
	}
	window.Tabs = Tabs;
})(window,$);
/*
*汽车图片小幅移动插件，用法： 
* $("div").leftMove();
*
*/	
(function($){
	$.fn.leftMove = function(options){
		var defaults = {
			time: 300,  //时间
			distance: 20  //位移距离  单位：px
		}
		var ops = $.extend(defaults,options);

		var time = parseInt(ops.time),
		oldDistance = parseInt($(this).css("margin-left").split("px")[0])||0,
		distance = oldDistance - parseInt(ops.distance);

		$(this).on("mouseover",function(){
			$(this).stop().animate({"margin-left": distance + "px"},time);
		})
		.on("mouseout",function(){
			$(this).stop().animate({"margin-left": oldDistance + "px"},time);
		});
			
	}
})(jQuery);

/** * 对Date的扩展，将 Date 转化为指定格式的String * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)
    可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new
    Date()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423      
 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04      
 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04      
 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04      
 * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
 */

Date.prototype.pattern = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, /**月份*/
		"d+": this.getDate(), /**日*/
		"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, /**小时 (12小时制)*/
		"H+": this.getHours(), /**小时 (24小时制)*/
		"m+": this.getMinutes(), /**分*/
		"s+": this.getSeconds(), /**秒*/
		"q+": Math.floor((this.getMonth() + 3) / 3), /**季度*/
		"S": this.getMilliseconds() /**毫秒*/
	};
	var week = {
		"0": "/u65e5",
		"1": "/u4e00",
		"2": "/u4e8c",
		"3": "/u4e09",
		"4": "/u56db",
		"5": "/u4e94",
		"6": "/u516d"
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	if (/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}

Date.prototype.getDayCount = function(year, month) {
	if (!year) {
		year = this.getFullYear();
		month = this.getMonth()+1;
	}

    var count = 0;
    if(month == 2){
        if((year%4 == 0 && year%100 !=0) || (year%400 ==0))
            count = 29;
        else
            count = 28;
    }
    else if(month==1 || month==3 || month==5 || month==7|| month==8 || month==10  || month==12)
        count = 31;
    else if(month==4 || month==6 || month==9 || month==11)
        count = 30;
    return count;
}
Date.prototype.equal = function(date) {
	if (this.getFullYear() == date.getFullYear() &&
		this.getMonth() == date.getMonth() &&
		this.getDate() == date.getDate())
		return true;
	else 
		return false;
}


